package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/19 11:24
 * description:
 */
@Service
public class DtsGoodsProductServiceImpl implements DtsGoodsProductService {

    @Autowired
    DtsGoodsProductMapper goodsProductMapper;

    @Override
    public Integer getTotal() {
        Integer count = goodsProductMapper.selectCount(null);

        return count;
    }

    @Override
    public void  updateProducts(Integer id, DtsGoodsProduct[] products) {
        //根据商品id删除
        LambdaQueryWrapper<DtsGoodsProduct> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId,id);
        goodsProductMapper.delete(qw);

        //添加新的库存数据
        if (products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                product.setDeleted(false);
                goodsProductMapper.insert(product);
            }
        }



    }

    @Override
    public void deteleByGoodsId(Integer id) {
        DtsGoodsProduct product=new DtsGoodsProduct();
        product.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsProduct> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId,id);
        goodsProductMapper.update(product,qw);
    }

    @Override
    public void addProduct(Integer id, DtsGoodsProduct[] products) {
        //添加新的库存数据
        if (products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                product.setDeleted(false);
                goodsProductMapper.insert(product);
            }
        }
    }
}
